<?php
include_once('../cnx.php');
include_once('../clases/Texto.php');
include_once('../clases/Archivo.php');
$limit = ($_POST['cuantos'])?$_POST['cuantos']:3;

$query = '
    select p.post_date as date, p.post_title as title, p.post_content as content, p.post_name as name from wp_posts as p 
    where p.post_type = "post" 
    and p.post_status = "publish" 
    and p.ID in (select object_id from wp_term_relationships as r where r.term_taxonomy_id = 68)
    order by p.post_date desc limit '.$limit
;
//term_taxonomy_id = 68 para post de chile y term_taxonomy_id = 69 para post de argentina
$posts = mysql_query($query);
if (!$posts) {die('Invalid query: ' . mysql_error());} 

$html = '';
$tpl = Archivo::leer('../../tpl/post.html');
while ($post = mysql_fetch_object($posts))  {
    $html .= $tpl;
    $html = str_replace('${postDate}', $post->date, $html);
    $html = str_replace('${postTitle}', htmlentities($post->title), $html);
    $html = str_replace('${postName}', $post->name, $html);
    $html = str_replace('${postContent}', htmlentities(Texto::cortar(cleanText($post->content), 233, '...')), $html);
    $html = str_replace('${postImage}', extractImage($post->content), $html);
}
$html = str_replace('${baseUrl}', 'http://www.dondereciclo.org.ar/blog/', $html);
echo($html);

function extractImage ($subject) {
    preg_match('/<img.*src="([a-zA-Z0-9-_\.:\/]+)".*>/', $subject, $results, PREG_OFFSET_CAPTURE);
    return $results[1][0];
}

function cleanText ($html) {
    $html = preg_replace('/<[^>]+>/', '', $html);
    return preg_replace('/\[.*\]/', '', $html);
}
?>
